#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

#include "coefficient.h"

using namespace std;

#define EPSILON 1e-9

/** @param str The string that contains the Coefficient.*/
Coefficient Coefficient::ReadCoefficientFromString(const string str) {
  istringstream iss(str);
  double coef;
  iss >> coef;
  return Coefficient(coef); 
}

/** @param c1 the coefficient for the operation.
 *  @return the result after applying the operation.
 */
Coefficient Coefficient::operator+(const Coefficient &c1) {
  return Coefficient(c1.coefficient_ + coefficient_);
}

/** @param c1 the coefficient for the operation.
 *  @return the result after applying the operation.
 */
Coefficient Coefficient::operator-(const Coefficient &c1) {
  return Coefficient(coefficient_-c1.coefficient_);
}

/** @param c1 the coefficient for the operation.
 *  @return the result after applying the operation.
 */
Coefficient Coefficient::operator/(const Coefficient &c1) {
  return Coefficient(coefficient_/c1.coefficient_);
}

/** @param c1 the coefficient for the operation.
 *  @return the result after applying the operation.
 */
Coefficient Coefficient::operator*(const Coefficient &c1) {
  return Coefficient(coefficient_*c1.coefficient_);
}

/** @return Coefficient instance with 0 value. */
Coefficient Coefficient::ZeroInstance() {
  Coefficient zero(0.);
  return zero;
}

/** @return Coefficient instance with value 1. */
Coefficient Coefficient::OneInstance() {
  Coefficient one(1.);
  return one;
}

/** @return true|false, wether the Coefficient value is 0 or not. */
bool Coefficient::NonZeroCoefficient() {
  return fabs(coefficient_) > EPSILON; 
}

// Hardcoded values for now.
void Coefficient::PrintCoefficientToStdout() {
  cout << fixed << setprecision(2) << coefficient_; 
}
